/**
*      author: Y_Yao
*      created: 2024-11-28 17:53:37
**/

#include<bits/stdc++.h>

using namespace std;
#define int LL
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int N = 1e7 + 7;
int T[N],M[N],dp[N];
int t,m;

void solve()
{
    cin >> t >> m;
    for(int i = 1;i <= m;i++)
    {
        cin >> T[i] >> M[i];
    }

    for(int i = 1;i <= m;i++)
    {
        for(int j = T[i];j <= t;j++)
        {
            if(j >= T[i])
            {
                dp[j] = max(dp[j],dp[j - T[i]] + M[i]);
            }
        }
    }

    cout << dp[t] << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}